import DS from 'ember-data';
import Ember from 'ember';
import {verifyEmail, verifyMobile, verifyUserrName} from '../utils/rutil';

export default DS.Model.extend({
    _patch: DS.attr(),
    name: DS.attr(),
    email: DS.attr(),
    mobile: DS.attr(),
    avatar: DS.attr(),
    enabled: DS.attr('boolean'),
    accountNonExpired: DS.attr('boolean'),
    accountNonLocked: DS.attr('boolean'),
    credentialsNonExpired: DS.attr('boolean'),
    emailVerified: DS.attr('boolean'),
    mobileVerified: DS.attr('boolean'),
    gender: DS.attr(),
    roles: DS.hasMany('role'),
    level: DS.attr(),
    createdAt: DS.attr('date'),
    password: DS.attr("string"),

    __reportPassword: "",

    __dspName: Ember.computed("name", function(){
      return this.get('name');
    }),

    validateMe() {
      var n = this.get('name') || "",
          email = this.get('email') || "",
          mobile = this.get('mobile'),
          code = this.get('code'),
          cas = this.changedAttributes(),
          ckeys = Object.keys(cas);

      if (!this.get('id') && (this.get('password') !== this.get("__reportPassword"))) {
        return {fn: "passwod", msg: "两次密码不匹配。"};
      }

      if (ckeys.length === 0) {
        return {fn: "整个", msg: "什么都没有改变，我就不向服务器保存了。"};
      }

      if (!verifyUserrName(n)) {
        return {fn: "名称", msg: "字母开头，可以包含字母和-_，长度在6-20"};
      }

      if (!verifyEmail(email)) {
        return {fn: "Email", msg: "Email格式错误"};
      }

      if (mobile && !verifyMobile(mobile)) {
        return {fn: "手机号码", msg: "手机号码格式错误"};
      }
      // if (code.length < 2) {
      //   return {fn: "Code", msg: "请输入Code，大于2个字符。"};
      // }
      return null;
    }
});
